package com.stx.openeyes.view.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.stx.openeyes.MainActivity;
import com.stx.openeyes.R;
import com.stx.openeyes.SealConst;
import com.stx.openeyes.dto.Response;
import com.stx.openeyes.model.User;
import com.stx.openeyes.utils.HttpAdress;
import com.stx.openeyes.utils.NToast;
import com.stx.openeyes.widget.LoadDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class FunctionActivity extends AppCompatActivity {

    private static final String TAG = "FunctionActivity";
    protected Context mContext;
    private Gson mGson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_function);
        StatusBarUtil.setTranslucent(this);
        ButterKnife.bind(this);
        mContext = this;
        setToolBar();
        // 检查登录
        checkLogin();
    }

    @SuppressLint("LongLogTag")
    private void checkLogin() {
        SharedPreferences settings = getSharedPreferences(SealConst.USER_INFO, 0);
        mGson = new Gson();
        String userInfo = settings.getString(SealConst.USER_INFO, null);
        Log.i("===check login local json", mGson.toJson(userInfo));
        if (TextUtils.isEmpty(userInfo))
            return;

        User user = mGson.fromJson(userInfo, User.class);
        if (user == null || TextUtils.isEmpty(user.getUuid()))
            return;

        // 拉取用户信息
        OkHttpUtils.get()
            .url(HttpAdress.USER_INFO)
            .addHeader("token", user.getUuid())
            .build()
            .execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    Log.i("===userInfo error", "msg is:" + e.getMessage());
                    NToast.shortToast(mContext, SealConst.NETWORK_ERROR);
                    LoadDialog.dismiss(mContext);
                }

                @Override
                public void onResponse(String response, int id) {
                    Log.i("===userInfo response json", response);
                    mGson = new Gson();
                    Response<User> userInfoResp = mGson.fromJson(response, Response.class);
                    if (!userInfoResp.isSuccess()) {
                        NToast.shortToast(mContext, userInfoResp.getMessage());
                        LoadDialog.dismiss(mContext);
                        return;
                    }
                    User userInfo = mGson.fromJson(mGson.toJson(userInfoResp.getData()), User.class);
                    TextView textView = (TextView) findViewById(R.id.user_mobile);
                    textView.setText(userInfo.getNickname());
                    textView.setClickable(false);
                    //显示修改密码
                    LinearLayout updatePassword = (LinearLayout) findViewById(R.id.xiugaimima);
                    updatePassword.setVisibility(View.VISIBLE);
                    // 显示退出登录
                    LinearLayout logoutTv = (LinearLayout) findViewById(R.id.logoutBtn);
                    logoutTv.setVisibility(View.VISIBLE);
                    // 修改头像
                    ImageView image = (ImageView) findViewById(R.id.user_icon);
                    image.setImageResource(R.mipmap.vatart);

                    LoadDialog.dismiss(mContext);
                }
            });
    }

    @OnClick({R.id.user_mobile, R.id.logoutBtn, R.id.xiugaimima})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_mobile:
                startActivity(new Intent(FunctionActivity.this, LoginActivity.class));
                break;
            case R.id.xiugaimima:
                startActivity(new Intent(FunctionActivity.this, UpdatePasswordActivity.class));
                break;
            case R.id.logoutBtn:
                // TODO: 调用退出登录接口
                OkHttpUtils.get()
                        .url(HttpAdress.EXITLOGIN)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {

                            }

                            @Override
                            public void onResponse(String response, int id) {

                            }
                        });



                SharedPreferences settings = getSharedPreferences(SealConst.USER_INFO, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = settings.edit();
                editor.clear();
                editor.commit();
                LoadDialog.show(mContext);
                TextView textView = (TextView) findViewById(R.id.user_mobile);
                textView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(R.string.click_login_text);
                        textView.setClickable(true);
                        ImageView image = (ImageView) findViewById(R.id.user_icon);
                        image.setImageResource(R.mipmap.tab_me);
                        LinearLayout logoutTv = (LinearLayout) findViewById(R.id.logoutBtn);
                        logoutTv.setVisibility(View.INVISIBLE);
                        LinearLayout updatepassword = (LinearLayout) findViewById(R.id.xiugaimima);
                        updatepassword.setVisibility(View.INVISIBLE);

                        LoadDialog.dismiss(mContext);
                    }
                }, 3000);
                break;
        }
    }

    private void setToolBar() {
        Toolbar tb = (Toolbar) findViewById(R.id.tb_function);
        setSupportActionBar(tb);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayShowTitleEnabled(false);
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(FunctionActivity.this, MainActivity.class));
            }
        });
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(FunctionActivity.this, MainActivity.class));
    }
}
